package com.ruoyi.wvp.controller;

import java.util.List;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;

import com.ruoyi.wvp.service.IDutyCountService;
import com.ruoyi.wvp.service.impl.DutyCountServiceImpl;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.wvp.domain.SurveillanceSupervisor;
import com.ruoyi.wvp.service.ISurveillanceSupervisorService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.enums.DeletedEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 督导员审核Controller
 * 
 * @author ruoyi
 * @date 2024-10-24
 */
@RestController
@RequestMapping("/wvp/supervisor")
@Tag(name = "【督导员审核】管理")
public class SurveillanceSupervisorController extends BaseController{
    @Autowired
    private ISurveillanceSupervisorService surveillanceSupervisorService;

    private final Map<String, ReentrantLock> locks = new ConcurrentHashMap<>();


    /**
     * 查询督导员审核列表
     */
    @Operation(summary = "查询督导员审核列表")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:list')")
    @GetMapping("/list")
    public TableDataInfo list(SurveillanceSupervisor surveillanceSupervisor) {
        Page<SurveillanceSupervisor> page = getPage();
        surveillanceSupervisorService.pageList(page, surveillanceSupervisor);
        return getTableDataInfo(page);
    }


    /**
     * 导出督导员审核列表
     */
    @Operation(summary = "导出督导员审核列表")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:export')")
    @Log(title = "督导员审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void exportAll(HttpServletResponse response,SurveillanceSupervisor surveillanceSupervisor){
        List<SurveillanceSupervisor> list = surveillanceSupervisorService.listAll(surveillanceSupervisor);
        list.forEach(item -> {
            if(item.getStatus().equals(1)){
                item.setStatusStr("在岗");
            }else if(item.getStatus().equals(2)){
                item.setStatusStr("离岗");
            }else if(item.getStatus().equals(3)){
                item.setStatusStr("无事件");
            }else{
                item.setStatusStr("待审核");
            }
        });
        ExcelUtil<SurveillanceSupervisor> util = new ExcelUtil<SurveillanceSupervisor>(SurveillanceSupervisor.class);
        util.exportExcel(response, list, "设备数据");
    }

    /**
     * 获取督导员审核详细信息
     */
    @Operation(summary = "获取督导员审核详细信息")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id){
        return success(surveillanceSupervisorService.getById(id));
    }

    /**
     * 新增督导员审核
     */
    @Operation(summary = "新增督导员审核")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:add')")
    @Log(title = "督导员审核", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SurveillanceSupervisor surveillanceSupervisor){
        surveillanceSupervisor.setDeleted(DeletedEnum.NOMAL.getValue());
        surveillanceSupervisor.setCreateBy(getUsername());
        surveillanceSupervisor.setCreateTime(new Date());
        return toAjax(surveillanceSupervisorService.save(surveillanceSupervisor));
    }

    /**
     * 修改督导员审核
     */
    @Operation(summary = "修改督导员审核")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:edit')")
    @Log(title = "督导员审核", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SurveillanceSupervisor surveillanceSupervisor){
         surveillanceSupervisor.setUpdateBy(getUsername());
         surveillanceSupervisor.setUpdateTime(new Date());
        return toAjax(surveillanceSupervisorService.updateById(surveillanceSupervisor));
    }

    /**
     * 删除督导员审核
     */
    @Operation(summary = "删除督导员审核")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:remove')")
    @Log(title = "督导员审核", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable( name = "ids" ) Long[] ids) {
        QueryWrapper<SurveillanceSupervisor> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id",List.of(ids));
        SurveillanceSupervisor surveillanceSupervisor = new SurveillanceSupervisor();
        surveillanceSupervisor.setUpdateTime(new Date());
        surveillanceSupervisor.setUpdateBy(getLoginUser().getUser().getUserName());
        surveillanceSupervisor.setDeleted(DeletedEnum.DELETED.getValue());
        return toAjax(surveillanceSupervisorService.update(surveillanceSupervisor,queryWrapper));
    }

    @Operation(summary = "确认在岗")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:success')")
    @Log(title = "确认在岗", businessType = BusinessType.UPDATE)
    @PostMapping("/onDuty/{id}")
    public  AjaxResult  onDuty(@PathVariable( name = "id" ) Long id) {
        ReentrantLock lock = locks.computeIfAbsent(id.toString(), key -> new ReentrantLock());
        lock.lock();
        try {
            // 业务逻辑
            surveillanceSupervisorService.onDuty(id);
        } finally {
            lock.unlock();
        }
        return toAjax(1);
    }

    @Operation(summary = "确认离岗")
    @PreAuthorize("@ss.hasPermi('wvp:supervisor:leaveDuty')")
    @Log(title = "确认离岗", businessType = BusinessType.UPDATE)
    @PostMapping("/leaveDuty/{id}")
    public AjaxResult leaveDuty(@PathVariable( name = "id" ) Long id) {

        return toAjax(surveillanceSupervisorService.leaveDuty(id));
    }

}
